Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix OptionsValidator source-gen to skip static and const members #88254

Merged
merged 5 commits into from
Jul 2, 2023
Merged

Fix OptionsValidator source-gen to skip static and const members #88254

merged 5 commits into from
Jul 2, 2023

Conversation

xakep139
Copy link
Contributor

@xakep139 xakep139 commented Jun 30, 2023

Fix #88150.
Also, performance of the generator was improved - now it doesn't check all members of string type.

@ghost
Copy link

ghost commented Jun 30, 2023

Tagging subscribers to this area: @dotnet/area-extensions-options
See info in area-owners.md if you want to be subscribed.

Issue Details

Fix #88150

Author: xakep139
Assignees: xakep139
Labels:

area-Extensions-Options

Milestone: -

@@ -462,7 +476,7 @@ private List<ValidatedMember> GetMembersToValidate(ITypeSymbol modelType, bool s
}

// generate a warning if the field/property seems like it should be enumerated
if (enumerationValidatorTypeName == null && speculate)
if (enumerationValidatorTypeName == null && speculate && memberType.SpecialType != SpecialType.System_String)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't check string internals anymore

@@ -59,7 +69,7 @@
</trans-unit>
<trans-unit id="MemberIsInaccessibleTitle">
<source>Can't validate private fields or properties.</source>
<target state="translated">Не удается проверить частные поля или свойства.</target>
<target state="translated">Не удается проверить приватные поля или свойства.</target>
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This fixes an incorrect translation.


if (member.IsStatic)
{
// generate an error if the member is static and has a validation attribute applied
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// generate an error if the member is static and has a validation attribute applied

Is it the correct thing to generate error and not just ignoring the statics?

Copy link
Contributor Author

@xakep139 xakep139 Jun 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can change this to warning, or completely ignore these cases. But it's better to notify a user about incorrect usage and that these members won't be validated.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be good if we check what is the runtime behavior when having the validation attribute on the statics.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

using a validation framework like ASP.NET MVC or ASP.NET Core, the RequiredAttribute alone would not automatically trigger validation for static properties. These frameworks primarily focus on validating instance properties of classes that are involved in HTTP request processing.

Considering that, I would say making it as warning will make sense.

@jeffhandley @geeknoid what do you think about that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turns out it was a warning already (bad copy-paste). Added an additional assertion in tests to ensure that

@tarekgh tarekgh added this to the 8.0.0 milestone Jun 30, 2023
@tarekgh
Copy link
Member

tarekgh commented Jun 30, 2023

Looks you missed adding one resources

src/libraries/Microsoft.Extensions.Options/gen/DiagDescriptors.cs(97,23): error CS0117: (NETCORE_ENGINEERING_TELEMETRY=Build) 'SR' does not contain a definition for 'CantValidateStaticOrConstMemberTitle'

@xakep139
Copy link
Contributor Author

Looks you missed adding one resources

src/libraries/Microsoft.Extensions.Options/gen/DiagDescriptors.cs(97,23): error CS0117: (NETCORE_ENGINEERING_TELEMETRY=Build) 'SR' does not contain a definition for 'CantValidateStaticOrConstMemberTitle'

And that's strange, because you can see I added it into src/libraries/Microsoft.Extensions.Options/gen/Resources/Strings.resx

@tarekgh
Copy link
Member

tarekgh commented Jun 30, 2023

The error is coming from the project Microsoft.Extensions.Options.SourceGeneration.Tests.csproj. You need to add it to https://github.com/dotnet/runtime/blob/main/src/libraries/Microsoft.Extensions.Options/tests/SourceGenerationTests/Resources/Strings.resx too. It is in my to do list to consolidate the resource files into global one.

Copy link
Member

@tarekgh tarekgh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @xakep139 providing the changes. LGTM

@tarekgh tarekgh merged commit 779d536 into dotnet:main Jul 2, 2023
@xakep139 xakep139 deleted the FixOptionsSourceGenStatics branch July 2, 2023 18:42
@ghost ghost locked as resolved and limited conversation to collaborators Aug 1, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

OptionsValidator source-gen shouldn't cover static and const members
2 participants